package com.xiaoshuidi.cloud.module.contract.controller.admin.fee;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.contract.controller.admin.fee.vo.FeeTypesCreateReqVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.fee.vo.FeeTypesRespVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.fee.vo.FeeTypesUpdateReqVO;
import com.xiaoshuidi.cloud.module.contract.convert.fee.FeeTypesConvert;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.fee.FeeTypesDO;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.service.fee.FeeTypesService;
import com.xiaoshuidi.cloud.module.contract.service.fee.FeesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 基本费项")
@RestController
@RequestMapping(ApiConstants.NAME+"/config/fee-types")
@Validated
public class FeeTypesController {

    @Resource
    private FeeTypesService feeTypesService;

    @Resource
    private FeesService feesService;

    @GetMapping("/get-name")
    @Operation(summary = "获得基本费项-根据name模糊查询")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<FeeTypesRespVO>> getFeeTypesByName(@RequestParam("feeName") String feeName) {
        List<FeeTypesDO> feeTypesByName = feeTypesService.getFeeTypesByName(feeName, Arrays.asList(0L, SecurityFrameworkUtils.getLoginUser().getTenantId()));
        return success(FeeTypesConvert.INSTANCE.convertList(feeTypesByName));
    }
    @GetMapping("/get")
    @Operation(summary = "获得基本费项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('config:fee-types:query')")
    public CommonResult<FeeTypesRespVO> getFeeTypes(@RequestParam("id") Long id) {
        FeeTypesDO feeTypes = feeTypesService.getFeeTypes(id);
        return success(FeeTypesConvert.INSTANCE.convert(feeTypes));
    }


    @PostMapping("/create")
    @Operation(summary = "创建基本费项")
    //@PreAuthorize("@ss.hasPermission('config:fee-types:create')")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult createFeeTypes(@Valid @RequestBody FeeTypesCreateReqVO createReqVO) {
        //费项是否存在
        if(createReqVO.getId()==null){
            FeeTypesDO feeTypes = feeTypesService.getFeeTypes(createReqVO.getFeeName());
            //相同费项的名称已经存在 设置已经查出的id
            if (feeTypes!=null) {
                createReqVO.setId(feeTypes.getId());
            }else {
                FeeTypesDO  feeTypesDO = feeTypesService.createFeeTypes(createReqVO);
                createReqVO.setId(feeTypesDO.getId());
            }
        }
        feesService.createFees(createReqVO);

        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新基本费项")
    //@PreAuthorize("@ss.hasPermission('config:fee-types:update')")
    public CommonResult<Boolean> updateFeeTypes(@Valid @RequestBody FeeTypesUpdateReqVO updateReqVO) {
        feeTypesService.updateFeeTypes(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除基本费项")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('config:fee-types:delete')")
    public CommonResult<Boolean> deleteFeeTypes(@RequestParam("id") Long id) {
        feeTypesService.deleteFeeTypes(id);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "获得基本费项列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    //@PreAuthorize("@ss.hasPermission('config:fee-types:query')")
    public CommonResult<List<FeeTypesRespVO>> getFeeTypesList(@RequestParam("ids") Collection<Long> ids) {
        List<FeeTypesDO> list = feeTypesService.getFeeTypesList(ids);
        return success(FeeTypesConvert.INSTANCE.convertList(list));
    }

    //    @PostMapping("/create")
//    @Operation(summary = "创建基本费项")
//    //@PreAuthorize("@ss.hasPermission('config:fee-types:create')")
//    public CommonResult<Long> createFeeTypes(@Valid @RequestBody FeeTypesCreateReqVO createReqVO) {
//        return success(feeTypesService.createFeeTypes(createReqVO));
//    }

}
